package com.pp.util.upload;

import com.pp.bean.UploadData;
import com.pp.bean.UploadResult;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @author 张子艺
 * @packge com.pp.util.upload
 * @data 2019-03-13 10:43
 * @project ppProject
 */
public class UploadFile {
    public static UploadResult upload(byte[] file, String fileName) throws Exception {
        String filePath = "c://";
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
        UploadData uploadData = new UploadData("/" + filePath + fileName);
        UploadResult uploadResult = new UploadResult();
        uploadResult.setCode(0);
        uploadResult.setMsg("上传成功");
        uploadResult.setData(uploadData);
        return uploadResult;
    }


    /**
     * @param file 文件
     * @return 上传成功返回 上传后的路径
     */
    public static String fileUpload(MultipartFile file) throws FileNotFoundException {
        if (file.isEmpty()) {
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();

        System.out.println(fileName + "-->" + size);

        String path = "F:/" ;
        // String path = "/home/project" ;
          String filePath = path+"/uploads/"+new Date().getTime()+fileName;
       /* File path   = new File(ResourceUtils.getURL("classpath:").getPath());
        String fiilePath=getJarRootPath()+"/uploads/"+new Date().getTime()+fileName;
        fiilePath=fiilePath.replace('\\','/');*/
        File dest = new File(filePath);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return filePath;
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 删除文件
     * @param path
     * @return
     */
    public static  boolean delFile(String path ) {
        File file=new File(path );
        if(file.exists()&&file.isFile() ){
            return  file.delete();
        }
        return  false;
    }
    public  static  void  main(String agrs[]) throws FileNotFoundException {
        getJarRootPath();
    }
    private static String getJarRootPath() throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        //=> file:/root/tmp/demo-springboot-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
        System.out.println("ResourceUtils.getURL(\"classpath:\").getPath() -> "+path);
        //创建File时会自动处理前缀和jar包路径问题 => /root/tmp
        File rootFile = new File(path);
        if(!rootFile.exists()) {
            System.out.println("根目录不存在, 重新创建");
            rootFile = new File("");
            System.out.println("重新创建的根目录: "+rootFile.getAbsolutePath()); }
        System.out.println("项目根目录: "+rootFile.getAbsolutePath());
        //获取的字符串末尾没有分隔符 /
        return rootFile.getAbsolutePath();
    }

}
